package com.cy.pj.notice.web.interceptor;

import org.hibernate.service.spi.ServiceException;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalTime;

public class TimeAccessInterceptor implements HandlerInterceptor {
    /**
     * 后端handler方法执行之前执行
     *
     * @param request  请求对象
     * @param response 响应对象
     * @param handler  目标处理器对象
     * @return true (放行,继续执行Handler方法) false(拦截)
     * @throws Exception 添加时间访问控制
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("==preHandle==");
        //JDK8获取当前时间,只有时分秒,DateTime.now()为年月日 时分秒
        LocalTime localTime = LocalTime.now();
        int hour = localTime.getHour();
        System.out.println(hour);
        if (hour <= 9 || hour > 18) {
            throw new ServiceException("请在指定时间访问"); }
            return true;
    }
}

